﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Way.EAP.DataAccess.Entity;
using Way.Bus.Model;
using System.Data;
using Way.EAP.DataAccess.Data;
using Way.Contract.MVC;

namespace Way.Bus.WMS.Model
{
    /// <summary>
    /// 描述：库存表
    /// 作者：liuqiang
    /// 创建日期:2012-01-09
    /// </summary>
    [EntityClassAttribut("Acc_WMS_InfoMaterials", "库存产品表", IsOnAppendProperty = true)]
    public class StockInfoMaterials : BasicInfo,IPropertyValueType
    {
        public StockInfoMaterials()
        {
            this.PORTCODE = "";
            this.DEPOTWBS = "";
        }
        /// <summary>
        /// 产品
        /// </summary>
        [EntityControl("产品", false, true, 1)]
        [EntityForeignKey(typeof(Materials), "ID", "FNAME")]
        [EntityField(100)]
        public new string Code { get; set; }


        /// <summary>
        /// 产品编码
        /// </summary>
        //[NotSearchAttribut]
        [EntityControl("产品编码", false, true, 2)]
        [EntityField(255)]
        public string MCODE { get; set; }

        /// <summary>
        /// 规格型号
        /// </summary>
        //[NotSearchAttribut]
        [EntityControl("规格型号", false, true, 3)]
        [EntityField(255)]
        public string FMODEL { get; set; }

        /// <summary>
        /// 是否锁定
        /// </summary>
        [EntityControl("是否锁定", false, true, 6)]
        [EntityField(255)]
        public bool ISLOCK { get; set; }

        /// <summary>
        /// 批次
        /// </summary>
        [EntityControl("批次", false, true, 4)]
        [EntityField(255)]
        public string BATCHNO { get; set; }

        /// <summary>
        /// 亚批次
        /// </summary>
        [EntityControl("亚批次", false, true, 7)]
        [EntityField(255)]
        public string SENBATCHNO { get; set; }


        /// <summary>
        /// 存储仓库
        /// </summary>
        [EntityControl("存储仓库", false, true, 8)]
        [EntityForeignKey(typeof(WareHouse), "ID", "WAREHOUSENAME")]
        [EntityField(255, IsNotNullable = true)]
        public int WAREHOUSEID { get; set; }

        //[EntityControl("仓库名称", false, true, 9)]
        //[EntityForeignKey(typeof(WareHouse), "ID", "WAREHOUSENAME")]
        //public new string STAY1 { get; set; }


        /// <summary>
        /// 存储位置
        /// </summary>
        [EntityControl("存储位置", false, true, 10)]
        [EntityForeignKey(typeof(WareHouse), "ID", "WAREHOUSENAME")]
        [EntityField(255)]
        public string DEPOTWBS { get; set; }

        /// <summary>
        /// 库存数量
        /// </summary>
        [EntityControl("库存数量", false, true,5)]
        [EntityField(30,Scale=2, IsNotNullable = true)]
        public double NUM { get; set; }

        /// <summary>
        /// 入库时间
        /// </summary>
        [EntityControl("入库时间",false,true,9)]
        [EntityField(255)]
        public DateTime LASTINTIME { get; set; }

        /// <summary>
        /// 出库时间
        /// </summary>
        [EntityControl("出库时间", false, true, 10)]
        [EntityField(255)]
        public DateTime LASTOUTTIME { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        [EntityControl("状态", false, true, 11)]
        [EntityField(255)]
        [ValueTypeProperty]
        public string STATUS { get; set; }

        
        /// <summary>
        /// 状态
        /// </summary>
        [NotSearchAttribut]
        [EntityControl("是否过期", false, true, 11)]
        [EntityField(255)]
        public string STATUS1 { get; set; }

        /// <summary>
        /// 销售区域
        /// </summary>
        [EntityControl("销售区域", false, true, 12)]
        [EntityField(255)]
        public string SALEAREA { get; set; }

        /// <summary>
        /// 托盘编码
        /// </summary>
        [EntityControl("托盘编码", false, true, 13)]
        [EntityForeignKey(typeof(Ports), "ID", "PORTNO")]
        [EntityField(255)]
        public string PORTCODE { get; set; }

        /// <summary>
        /// 入库仓库
        /// </summary>
        //[EntityControl("入库仓库", false, true, 14)]
        //[EntityField(255)]
        //public string HOUSECODE { get; set; }

        /// <summary>
        /// 入库单号
        /// </summary>
        [EntityControl("入库单号", false, true, 15)]
        [EntityField(255)]
        public string ORDERNO { get; set; }


        /// <summary>
        /// 冻结数量
        /// </summary>
        [EntityControl("冻结数量",false,true,16)]
        [EntityField(30,Scale=2)]
        public double FREEZENUM { get; set; }

        PropertyValueType[] IPropertyValueType.GetValueType(ValueTypeArgs e)
        {
            List<PropertyValueType> list = new List<PropertyValueType>();
            if (e.ColumnName == "STATUS")
            {
                PropertyValueType vt = new PropertyValueType();
                vt.Text = "待检验";
                vt.Value = "1";
                list.Add(vt);

                PropertyValueType vt1 = new PropertyValueType();
                vt1.Text = "合格";
                vt1.Value = "2";
                list.Add(vt1);

                PropertyValueType vt2 = new PropertyValueType();
                vt2.Text = "不合格";
                vt2.Value = "3";
                list.Add(vt2);

                PropertyValueType vt3 = new PropertyValueType();
                vt3.Text = "免质检";
                vt3.Value = "4";
                list.Add(vt3);
                //hidisger service 微软大数据展示
            }

            return list.ToArray();
        }

        //protected override string GetSearchSQL()
        //{
        //    string n = this.ToString();
        //    string sql = "select " + n + ".*,Acc_Bus_BusinessCommodity.FMODEL as FMODEL,Acc_Bus_BusinessCommodity.Code as MCODE  from (" + base.GetSearchSQL() + ") " + n + "  left join Acc_Bus_BusinessCommodity on " + n + ".Code=Acc_Bus_BusinessCommodity.ID";
        //    return sql;
        //}

        private HierarchicalEntityView<StockInfoMaterials, InfoInSequence> _StockSequence;
        /// <summary>
        /// 序列码
        /// </summary>
        public HierarchicalEntityView<StockInfoMaterials, InfoInSequence> StockSequence
        {
            get
            {
                if (_StockSequence == null)
                {
                    _StockSequence = new HierarchicalEntityView<StockInfoMaterials, InfoInSequence>(this);
                }
                return _StockSequence;
            }
        }
        /// <summary>
        /// 获取库存+序列号
        /// </summary>
        /// <param name="SeqCode">序列号</param>
        /// <returns></returns>
        public static StockInfoMaterials GetStock(string SeqCode)
        {
            InfoInSequence iis = new InfoInSequence();
            string seqsql = iis.ToString();
            string sql = "select * from " + seqsql + " where SEQUENCECODE='" + SeqCode + "'";
            DataTable table = iis.GetDataAction().GetDataTable(sql);
            if (table != null && table.Rows.Count > 0)
            {
                string id = table.Rows[0]["STOCKINFOMATERIALSID"].ToString();
                EntityList<StockInfoMaterials> list = new EntityList<StockInfoMaterials>(iis.GetDataAction());
                list.GetData("id=" + id);
                if (list.Count > 0)
                {
                    iis.STOCKINFOMATERIALSID = int.Parse(id);
                    iis.SEQUENCECODE = SeqCode;
                    list[0].StockSequence.Add(iis);
                    return list[0];
                }
                return null;
            }
            return null;
        }
    }
    /// <summary>
    /// 库存视图
    /// </summary>
    public class StockInfoView : BusinessBase
    {
       
        public StockInfoView()
        {
            this.SortIndex = "BATCHNO";
        }
        /// <summary>
        /// 客户ID
        /// </summary>
        [EntityForeignKey(typeof(BusinessCustomer), "ID", "CUSTOMERNAME")]
        public int KID { get; set; }
        /// <summary>
        /// 类别ID
        /// </summary>
        public int LID { get; set; }
        /// <summary>
        /// 商品ID
        /// </summary>
        [EntityForeignKey(typeof(Materials), "ID", "FNAME")]
        public int MID { get; set; }
        /// <summary>
        /// 仓库ID
        /// </summary>
        [EntityForeignKey(typeof(WareHouse), "ID", "WAREHOUSENAME")]
        public int WID { get; set; }
        /// <summary>
        /// 货位ID
        /// </summary>
        [EntityControl("货位ID", false, false, 0)]
        public int DID { get; set; }
        /// <summary>
        /// 客户名称
        /// </summary>
        [EntityControl("客户名称", false, true, -1)]
        public string KNAME { get; set; }
        /// <summary>
        /// 商品类别
        /// </summary>
        [EntityControl("商品类别", false, true, 0)]
        public string LNAME { get; set; }
        /// <summary>
        /// 商品编码
        /// </summary>
        [EntityControl("商品编码", false, true, 1)]
        public string MCODE { get; set; }
        /// <summary>
        /// 商品名称
        /// </summary>
        [EntityControl("商品名称", false, true, 2)]
        public string MNAME { get; set; }
        /// <summary>
        /// 规格型号
        /// </summary>
        [EntityControl("规格型号", false, true, 3)]
        public string model { get; set; }
        /// <summary>
        /// 库名称
        /// </summary>
        [EntityControl("仓库", false, true, 4)]
        public string WNAME { get; set; }
        /// <summary>
        /// 货区名称
        /// </summary>
        [EntityControl("货区", false, true, 5)]
        public string WAREHOUSENAME { get; set; }
        /// <summary>
        /// 货位编码
        /// </summary>
        //[EntityForeignKey(typeof(WareHouse), "CODE", "CODE")]
        [EntityControl("货位编码", false, true, 6)]
        public string DCODE { get; set; }
        /// <summary>
        /// 批次号
        /// </summary>
        [EntityControl("批次号", false, true, 7)]
        public string BATCHNO { get; set; }
        /// <summary>
        /// 亚批次号
        /// </summary>
        [EntityControl("亚批次号", false, true, 8)]
        public string SUBNO { get; set; }
        /// <summary>
        /// 可用数量
        /// </summary>
        [EntityControl("可用数量", false, true, 9)]
        public double KYNUM { get; set; }
        /// <summary>
        /// 库存数量
        /// </summary>
        [EntityControl("库存数量", false, true, 10)]
        public double NUM { get; set; }
        /// <summary>
        /// 占用数量
        /// </summary>
        [EntityControl("占用数量", false, true, 11)]
        public double ZYNUM { get; set; }
        /// <summary>
        /// 更新日期
        /// </summary>
        [EntityControl("更新日期", false, true, 12)]
        public virtual DateTime lastintime { get; set; }
        /// <summary>
        /// 商品备注
        /// </summary>
        [NotSearchAttribut]
        [EntityControl("商品备注", false, true, 13)]
        public string spmark { get; set; }

        private HierarchicalEntityView<StockInfoView, InfoInSequence> _StockSequence;
        /// <summary>
        /// 序列码
        /// </summary>
        [HierarchicalEntityControl(isadd = false, isedit = true, isremove = false)]
        public HierarchicalEntityView<StockInfoView, InfoInSequence> StockSequence
        {
            get
            {
                if (_StockSequence == null)
                {
                    _StockSequence = new HierarchicalEntityView<StockInfoView, InfoInSequence>(this);
                    //_StockSequence.AttachCondition("ID", "STOCKINFOMATERIALSID");

                }
                return _StockSequence;
            }
        }

        public string getGetSearchSQL()
        {
            return GetSearchSQL();
        }

        protected override string GetSearchSQL()
        {
            //Materials m = this.GetForeignObject<Materials>(this.GetDataAction());
            //if (!m.BATCH)
            //    this.BATCHNO = "";
            //if (!m.ISPERIOD)
            //    this.SUBNO = "";
            string sql = GetStockSQL(0, this.WID, this.MID, this.DID, this.BATCHNO,this.SUBNO);
            sql = "select * from (" + sql + ") StockInfoView";
            return sql;
        }
        /// <summary>
        /// 获取库存数量及库存占用数量
        /// </summary>
        /// <param name="orderid">主单ID</param>
        /// <param name="wid">仓库ID</param>
        /// <param name="mid">商品ID</param>
        /// <param name="did">货位ID</param>
        /// <param name="batchno">批次</param>
        /// <returns></returns>
        public static string GetStockSQL(int orderid,int wid,int mid,int did,string batchno)
        {
            return GetStockSQL(orderid, wid, mid, did, batchno, "");
        }
        public static string GetStockSQL(int orderid, int wid, int mid, int did, string batchno, string subno)
        {
            string sql = "select *,(a.num-a.zynum) kynum from ("
                + " select a.*,case when b.NUM is null then 0 else b.num end zynum from ("
                + " select b.*,a.ID,a.BATCHNO batchno,a.SENBATCHNO subno,a.WAREHOUSEID wid,c.Code wcode, c.WAREHOUSENAME wname, a.DEPOTWBS did,d.code dcode,e.WAREHOUSENAME WAREHOUSENAME,a.NUM num,a.STATUS,a.LASTINTIME ctime,a.Remark remark from"
                + " Acc_WMS_InfoMaterials a"
                + " inner join (select a.STAY10 kid,c.customername kname,a.ClassID lid,b.ClassName lname,a.ID mid,a.code mcode,a.FNAME mname,a.FMODEL model,a.remark spmark from Acc_Bus_BusinessCommodity a"
                + " left join Acc_Bus_CommodityClass b"
                + " on a.ClassID=b.ID"
                + " left join Acc_Bus_BusinessCustomer c"
                + " on a.STAY10=c.ID) b"
                + " on a.Code=b.mid"
                + " left join Acc_WMS_WareHouse c"
                + " on a.WAREHOUSEID=c.id"
                + " left join Acc_WMS_WareHouse d"
                + " on a.DEPOTWBS=d.id left join (select a.id,b.WAREHOUSENAME from Acc_WMS_WareHouse a"
                + " left join Acc_WMS_WareHouse b"
                + " on b.ID=a.PARENTID) e"
                + " on a.DEPOTWBS=e.id ) a"
                + " left join (select a.MATERIALCODE mid,a.BATCHNO batchno,a.DEPOTWBS did,a.senbatchno subno,SUM(a.num) NUM from Acc_WMS_OutOrderMaterials a,"
                + " Acc_WMS_OutOrder b where a.PARENTID=b.ID and b.IsSubmited=0 and a.PARENTID<>" + orderid
                + " group by a.MATERIALCODE,a.BATCHNO,a.DEPOTWBS,a.senbatchno) b"
                + " on a.mid=b.mid and a.did=b.did and a.batchno=b.batchno and a.subno=b.subno"
                + " ) a";
            string w = " where 1=1";
            if (mid > 0)
                w += " and MID=" + mid;
            if (wid > 0)
                w += " and WID=" + wid;
            if (did > 0)
                w += " and DID=" + did;
            if (!string.IsNullOrEmpty(batchno))
                w += " and BATCHNO='" + batchno + "'";
            if (!string.IsNullOrEmpty(subno))
                w += " and subno='" + subno + "'";
            sql = sql + w;
            return sql;
        }

    }
}

